package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IMyCenterFragmentModel;
import com.wgts.wgts.mvp.model.IPutForwardActivityModel;
import com.wgts.wgts.mvp.presenter.IPutForwardActivityPresenter;
import com.wgts.wgts.mvp.view.IPutForwardActivityView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.DelPutForwardReq;
import com.wgts.wgts.netmodle.req.UserPutForwardReq;
import com.wgts.wgts.netmodle.resp.CurrentEntity;
import com.wgts.wgts.netmodle.resp.MyCenterMessageEntity;
import com.wgts.wgts.netmodle.resp.PutForwardTypeEntity;

import java.util.List;

public class PutForwardActivityPresenter implements IPutForwardActivityPresenter {

    private IPutForwardActivityModel model;

    private IPutForwardActivityView view;

    private IMyCenterFragmentModel myCenterModel;

    private List<PutForwardTypeEntity> entityList;


    public PutForwardActivityPresenter(IPutForwardActivityModel model, IPutForwardActivityView view, IMyCenterFragmentModel myCenterModel) {
        this.model = model;
        this.view = view;
        this.myCenterModel = myCenterModel;
    }


    @Override
    public void initPutForward() {
        model.initPutForward(new HttpResultSubscriber<List<PutForwardTypeEntity>>() {
            @Override
            public void onSuccess(List<PutForwardTypeEntity> entities) {
                entityList = entities;

                view.initPutForwardList(entities);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void putForward(String money, String cardId, String payPassword) {
        UserPutForwardReq req = new UserPutForwardReq();
        req.money = money;
        req.cardId = cardId;
        req.payPassword = payPassword;
        model.putForward(req, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                view.putForwardSuccess();
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void delPutForward(final int i) {
        DelPutForwardReq req = new DelPutForwardReq();
        req.cardId = entityList.get(i).cardId;
        model.delPutForWard(req, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                entityList.remove(i);
                view.delPutForward(i);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });

    }

    @Override
    public void selectPutForward(int i) {
        for(PutForwardTypeEntity entity : entityList){
            entity.setChoese(false);
        }

        entityList.get(i).setChoese(!entityList.get(i).isChoese());
        view.selectPutForwardType(entityList.get(i));
    }

    @Override
    public void userInfo() {
        myCenterModel.getMyCenterResult(new HttpResultSubscriber<MyCenterMessageEntity>() {
            @Override
            public void onSuccess(MyCenterMessageEntity myCenterMessageEntity) {
                view.userInfo(myCenterMessageEntity);
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }


}
